package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.R;

public class GuideIndicator extends RelativeLayout implements ViewPager.OnPageChangeListener {

    private int mPosition;

    private ImageView mImage;

    public GuideIndicator(Context context) {
        super(context);
        init(context);
    }

    public GuideIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GuideIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        switch (position) {
            case 0:
                mImage.setTranslationX((getWidth() - mImage.getWidth()) / 2 * positionOffset);
                break;
            case 1:
                mImage.setTranslationX(getWidth() / 2 - mImage.getWidth() / 2 + ((getWidth() - mImage.getWidth()) / 2 * positionOffset));
                break;
            case 2:
                mImage.setTranslationX(getWidth() - mImage.getWidth() + ((getWidth() - mImage.getWidth()) / 2 * positionOffset));
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                mImage.setX(0);
                break;
            case 1:
                mImage.setX((getWidth() - mImage.getWidth()) / 2);
                break;
            case 2:
                mImage.setX(getWidth() - mImage.getWidth());
                break;
        }
        mPosition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (mPosition == 2 && state == ViewPager.SCROLL_STATE_IDLE) {
            setVisibility(View.INVISIBLE);
        } else {
            setVisibility(View.VISIBLE);
        }
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_guide_indicator, this, true);
        mImage = (ImageView) findViewById(R.id.image_point);
    }
}
